package threadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-12-20 14:28
 **/
public class T10_FixedThreadPool {


    public static void main(String[] args) {

        /**
         * ==========> Executors.newFixedThreadPool(10);
         * --------------------------------------------------
         * 固定线程数的线程池
         *
         *    public static ExecutorService newFixedThreadPool(int nThreads) {
         *         return new ThreadPoolExecutor(nThreads, nThreads,
         *                                       0L, TimeUnit.MILLISECONDS,
         *                                       new LinkedBlockingQueue<Runnable>());
         *     }
         */
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 20; i++) {
            final int j = i;
            executorService.execute(() -> {
                System.err.println(Thread.currentThread().getName() + "==========> " + j);
                try {
                    /**
                     * 所有线程执行打印之后占用当前线程一秒钟
                     * 线程池归固定只能有十个线程，但是这里有二十个线程
                     * 并且每个线程执行完都会占用一秒，
                     * --->  有是个线程一定会等其他十个线程在一秒钟执行完成之后才有执行的机会
                     */
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        executorService.shutdown();

        /**
         * 半秒钟之后打印分界线
         */
        new Thread(() -> {
            try {
                Thread.sleep(500);
                System.err.println();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.err.println("************************");
        }).start();
    }
}
